package me.flyray.bsin.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.context.BsinServiceContext;
import me.flyray.bsin.domain.entity.CustomerPassCard;
import me.flyray.bsin.domain.entity.DidProfile;
import me.flyray.bsin.facade.engine.DidProfileServiceEngine;
import me.flyray.bsin.facade.engine.PassCardServiceEngine;
import me.flyray.bsin.facade.engine.WalletServiceEngine;
import me.flyray.bsin.facade.response.PassCardVo;
import me.flyray.bsin.facade.service.CustomerPassCardService;
import me.flyray.bsin.infrastructure.mapper.CustomerPassCardMapper;
import me.flyray.bsin.infrastructure.mapper.DidProfileMapper;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@ShenyuDubboService(path = "/customerPassCard", timeout = 6000)
@ApiModule(value = "customerPassCard")
public class CustomerPassCardServiceImpl implements CustomerPassCardService {

    @Autowired
    private DidProfileServiceEngine didProfileServiceEngine;
    @Autowired
    private PassCardServiceEngine passCardServiceEngine;
    @Autowired
    private WalletServiceEngine walletServiceEngine;
    @Autowired
    private CustomerPassCardMapper customerPassCardMapper;
    @Autowired
    private DidProfileMapper didProfileMapper;

    @ShenyuDubboClient("/getDetail")
    @ApiDoc(desc = "getDetail")
    @Override
    public PassCardVo getDetail(Map<String, Object> requestMap) {


        return null;
    }

    /**
     * 查询我的卡包列表 查询我参与的共建品牌
     * @param requestMap
     * @return
     */
    @ShenyuDubboClient("/getList")
    @ApiDoc(desc = "getList")
    @Override
    public List<CustomerPassCard> getList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        // 传值用户优先，否则使用登录用户
        String customerNo = StringUtils.defaultIfBlank(
            (String) requestMap.get("customerNo"), loginUser.getCustomerNo());
        // 获取前端传的商户
        String merchantNo = MapUtils.getString(requestMap, "merchantNo");
        String tenantId = StringUtils.defaultIfBlank(
            MapUtils.getString(requestMap, "tenantId"), loginUser.getTenantId());
        CustomerPassCard customerPassCardReq =
                BsinServiceContext.getReqBodyDto(CustomerPassCard.class, requestMap);
        List<CustomerPassCard> customerPassCardList =
                customerPassCardMapper.selectCustomerPassCardList(tenantId, merchantNo, customerNo);

        // 添加卡DID信息
        LambdaQueryWrapper<DidProfile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DidProfile::getBizRoleTypeNo, loginUser.getBizRoleTypeNo());
        DidProfile didProfile = didProfileMapper.selectOne(queryWrapper);

        List<String> merchantNos = new ArrayList<>();
        for (CustomerPassCard customerPassCard : customerPassCardList) {
            // 获取商户信息
            merchantNos.add(customerPassCard.getMerchantNo());
            if (didProfile != null) {
                customerPassCard.setDid(didProfile.getDid());
            }
        }
        if (merchantNos.size() > 0) {
            Map marketReqMap = new HashMap();
            marketReqMap.put("merchantNos", merchantNos);
//      List<Merchant> merchantList = merchantService.getListByMerchantNos(marketReqMap);
//      for (CustomerPassCard customerPassCard : customerPassCardList) {
//        // 找出客户信息
//        for (Merchant merchant : merchantList) {
//          if (customerPassCard.getMerchantNo().equals(merchant.getSerialNo())) {
//            customerPassCard.setMerchantName(merchant.getMerchantName());
//            customerPassCard.setMerchantLogo(merchant.getLogoUrl());
//          }
//        }
//      }
        }
        return customerPassCardList;
    }

}
